package com.beemans.common.utils

import com.beemans.common.data.bean.AdCodeResponse
import com.blankj.utilcode.constant.TimeConstants
import com.blankj.utilcode.util.TimeUtils

/**
 * @author tiamosu
 * @date 2021/3/21
 */
object CommonCacheUtils {
    private const val KEY_CAL_DEVICE_ID = "KEY_CAL_DEVICE_ID"   //设备唯一标识码
    private const val LAST_VERSION_NAME = "LAST_VERSION_NAME"   //通过版本号判断是否清除Glide缓存
    private const val TP_AD_CONFIG = "TP_AD_CONFIG"             //TopOn广告位Id配置

    private const val APP_FIRST_OPEN = "APP_FIRST_OPEN"         //记录APP第一次打开的时间

    private const val REWARD_REQUEST_COUNT = "REWARD_REQUEST_COUNT"
    private const val REWARD_SHOW_COUNT = "REWARD_SHOW_COUNT"
    private const val REWARD_CLICK_COUNT = "REWARD_CLICK_COUNT"

    private const val SPLASH_REQUEST_COUNT = "SPLASH_REQUEST_COUNT"
    private const val SPLASH_SHOW_COUNT = "SPLASH_SHOW_COUNT"
    private const val SPLASH_CLICK_COUNT = "SPLASH_CLICK_COUNT"

    private const val INSERT_REQUEST_COUNT = "INSERT_REQUEST_COUNT"
    private const val INSERT_SHOW_COUNT = "INSERT_SHOW_COUNT"
    private const val INSERT_CLICK_COUNT = "INSERT_CLICK_COUNT"

    private const val NATIVE_REQUEST_COUNT = "NATIVE_REQUEST_COUNT"
    private const val NATIVE_SHOW_COUNT = "NATIVE_SHOW_COUNT"
    private const val NATIVE_CLICK_COUNT = "NATIVE_CLICK_COUNT"

    private const val BANNER_REQUEST_COUNT = "BANNER_REQUEST_COUNT"
    private const val BANNER_SHOW_COUNT = "BANNER_SHOW_COUNT"
    private const val BANNER_CLICK_COUNT = "BANNER_CLICK_COUNT"

    private const val UM_OAID = "UM_OAID"           //友盟oaid
    private const val UPLOAD_EQUIP = "NEW_UPLOAD_EQUIP" //上报设备信息
    private const val UPLOAD_EQUIP_WITH_UPID = "UPLOAD_EQUIP_WITH_UPID" //上报设备信息，带有TopOn参数UP_ID
    private const val TMP_STATIC_UPLOAD_USER = "TMP_STATIC_UPLOAD_USER" //服务端埋点上报先调用user接口

    /**
     * 获取设备唯一标识码
     */
    var deviceId: String?
        get() = MmkvHelper.getString(KEY_CAL_DEVICE_ID)
        set(value) = MmkvHelper.putString(KEY_CAL_DEVICE_ID, value)

    /**
     * 记录上一次APP版本号
     */
    var lastVersionName: String
        get() = MmkvHelper.getString(LAST_VERSION_NAME) ?: ""
        set(value) = MmkvHelper.putString(LAST_VERSION_NAME, value)

    /**
     * 打开应用的第几天
     */
    val openAppDay: Int
        get() {
            val firstOpenTime = MmkvHelper.getLong(APP_FIRST_OPEN)
            if (firstOpenTime == 0L) {
                MmkvHelper.putLong(APP_FIRST_OPEN, System.currentTimeMillis())
                return 1
            }
            var timeSpan = 0
            kotlin.runCatching {
                timeSpan = TimeUtils.getTimeSpan(
                    System.currentTimeMillis(), firstOpenTime, TimeConstants.DAY
                ).toInt()
                if (timeSpan <= 0) {
                    timeSpan = 0
                }
                timeSpan += 1
            }
            return timeSpan
        }

    /**
     * 获取topon广告位配置
     */
    var tpAdConfig: AdCodeResponse
        get() = MmkvHelper.getParcelable(TP_AD_CONFIG, AdCodeResponse::class.java)
            ?: AdCodeResponse()
        set(value) = MmkvHelper.putParcelable(TP_AD_CONFIG, value)

    /**
     * 激励视频请求次数
     */
    var rewardRequestCount: Int
        get() = MmkvHelper.getInt(REWARD_REQUEST_COUNT)
        set(value) = MmkvHelper.putInt(REWARD_REQUEST_COUNT, value)

    /**
     * 激励视频展示次数
     */
    var rewardShowCount: Int
        get() = MmkvHelper.getInt(REWARD_SHOW_COUNT)
        set(value) = MmkvHelper.putInt(REWARD_SHOW_COUNT, value)

    /**
     * 激励视频点击次数
     */
    var rewardClickCount: Int
        get() = MmkvHelper.getInt(REWARD_CLICK_COUNT)
        set(value) = MmkvHelper.putInt(REWARD_CLICK_COUNT, value)

    /**
     * 开屏请求次数
     */
    var splashRequestCount: Int
        get() = MmkvHelper.getInt(SPLASH_REQUEST_COUNT)
        set(value) = MmkvHelper.putInt(SPLASH_REQUEST_COUNT, value)

    /**
     * 开屏展示次数
     */
    var splashShowCount: Int
        get() = MmkvHelper.getInt(SPLASH_SHOW_COUNT)
        set(value) = MmkvHelper.putInt(SPLASH_SHOW_COUNT, value)

    /**
     * 开屏点击次数
     */
    var splashClickCount: Int
        get() = MmkvHelper.getInt(SPLASH_CLICK_COUNT)
        set(value) = MmkvHelper.putInt(SPLASH_CLICK_COUNT, value)

    /**
     * 插屏请求次数
     */
    var insertRequestCount: Int
        get() = MmkvHelper.getInt(INSERT_REQUEST_COUNT)
        set(value) = MmkvHelper.putInt(INSERT_REQUEST_COUNT, value)

    /**
     * 插屏展示次数
     */
    var insertShowCount: Int
        get() = MmkvHelper.getInt(INSERT_SHOW_COUNT)
        set(value) = MmkvHelper.putInt(INSERT_SHOW_COUNT, value)

    /**
     * 插屏点击次数
     */
    var insertClickCount: Int
        get() = MmkvHelper.getInt(INSERT_CLICK_COUNT)
        set(value) = MmkvHelper.putInt(INSERT_CLICK_COUNT, value)

    /**
     * 信息流请求次数
     */
    var nativeRequestCount: Int
        get() = MmkvHelper.getInt(NATIVE_REQUEST_COUNT)
        set(value) = MmkvHelper.putInt(NATIVE_REQUEST_COUNT, value)

    /**
     * 信息流展示次数
     */
    var nativeShowCount: Int
        get() = MmkvHelper.getInt(NATIVE_SHOW_COUNT)
        set(value) = MmkvHelper.putInt(NATIVE_SHOW_COUNT, value)

    /**
     * 信息流点击次数
     */
    var nativeClickCount: Int
        get() = MmkvHelper.getInt(NATIVE_CLICK_COUNT)
        set(value) = MmkvHelper.putInt(NATIVE_CLICK_COUNT, value)

    /**
     * Banner请求次数
     */
    var bannerRequestCount: Int
        get() = MmkvHelper.getInt(BANNER_REQUEST_COUNT)
        set(value) = MmkvHelper.putInt(BANNER_REQUEST_COUNT, value)

    /**
     * Banner展示次数
     */
    var bannerShowCount: Int
        get() = MmkvHelper.getInt(BANNER_SHOW_COUNT)
        set(value) = MmkvHelper.putInt(BANNER_SHOW_COUNT, value)

    /**
     * Banner点击次数
     */
    var bannerClickCount: Int
        get() = MmkvHelper.getInt(BANNER_CLICK_COUNT)
        set(value) = MmkvHelper.putInt(BANNER_CLICK_COUNT, value)

    /**
     * 获取友盟oaid
     */
    var umOaid: String
        get() = MmkvHelper.getString(UM_OAID) ?: ""
        set(value) = MmkvHelper.putString(UM_OAID, value)

    /**
     * 是否上报设备信息
     */
    var isUploadEquip: Boolean
        get() = MmkvHelper.getBool(UPLOAD_EQUIP, true)
        set(value) = MmkvHelper.putBool(UPLOAD_EQUIP, value)

    /**
     * 是否上报设备信息，带有TopOn参数UP_ID
     */
    var isUploadEquipWithUpId: Boolean
        get() = MmkvHelper.getBool(UPLOAD_EQUIP_WITH_UPID, true)
        set(value) = MmkvHelper.putBool(UPLOAD_EQUIP_WITH_UPID, value)

    /**
     * 服务端埋点上报先调用user接口
     */
    var isTmpStaticUploadInfo: Boolean
        get() = MmkvHelper.getBool(TMP_STATIC_UPLOAD_USER, true)
        set(value) = MmkvHelper.putBool(TMP_STATIC_UPLOAD_USER, value)
}